home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / ghostscr / gs252ini.zip / GS_FONTS.PS < prev    next >
Text File  |  1992-09-10  |  15KB  |  464 lines

  1. %    Copyright (C) 1990, 1992 Aladdin Enterprises.  All rights reserved.
  2. %    Distributed by Free Software Foundation, Inc.
  3. %
  4. % This file is part of Ghostscript.
  5. %
  6. % Ghostscript is distributed in the hope that it will be useful, but
  7. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. % to anyone for the consequences of using it or for whether it serves any
  9. % particular purpose or works at all, unless he says so in writing.  Refer
  10. % to the Ghostscript General Public License for full details.
  11. %
  12. % Everyone is granted permission to copy, modify and redistribute
  13. % Ghostscript, but only under the conditions described in the Ghostscript
  14. % General Public License.  A copy of this license is supposed to have been
  15. % given to you along with Ghostscript so you can know your rights and
  16. % responsibilities.  It should be in a file named COPYING.  Among other
  17. % things, the copyright notice and this notice must be preserved on all
  18. % copies.
  19.  
  20. % Font initialization for Ghostscript.
  21.  
  22. % Ghostscript fonts have essentially the same contents as Adobe Type 1 fonts,
  23. % except that the external form doesn't use eexec encryption.
  24. % Someday there may be GNU documentation that describes this format.
  25. % Until then, you'll have to either get a copy of Adobe's book, or read
  26. % the Ghostscript code.  The interpreter for Type 1 fonts, which reveals
  27. % most of their structure, is in the file gstype1.c.
  28.  
  29.  
  30. % Define the default font.
  31. /defaultfontname /Ugly def
  32.  
  33. % Load the font name -> font file name map.
  34. FontDirectory maxlength dict
  35. 2 dict begin
  36.   mark 1 index
  37.   /;
  38.    { % The stack contains a mark, the dictionary, the font name,
  39.      % the file or alias name, and possibly additional information
  40.      % about the font.
  41.      counttomark 3 sub { pop } repeat put
  42.      1 index
  43.    } bind def
  44.   (Fontmap) run
  45. end
  46. pop pop        % pop the mark and the copy of the dictionary
  47. userdict exch /Fontmap exch put
  48.  
  49.  
  50. % Ghostscript optionally can load individual CharStrings as they are needed.
  51. % (This is intended primarily for machines with very small memories.)
  52. % This happens if DISKFONTS is true.  In this case, we define another
  53. % dictionary parallel to FontDirectory that retains an open file
  54. % for every font loaded.
  55. DISKFONTS
  56.  { /FontFileDirectory FontDirectory maxlength dict def
  57.  }
  58. if
  59.  
  60.  
  61. % Define an augmented version of .buildfont1 that inserts UnderlinePosition
  62. % and UnderlineThickness entries in FontInfo if they aren't there already.
  63. % (This works around the incorrect assumption, made by many word processors,
  64. % that these entries are present in the built-in fonts.)
  65. /.buildfont1x
  66.  { dup /FontInfo known not
  67.     { dup /FontInfo 2 dict put }
  68.    if
  69.    dup dup /FontInfo get dup dup
  70.    /UnderlinePosition known exch /UnderlineThickness known and
  71.     { pop pop        % entries already present
  72.     }
  73.     { dup length 2 add dict copy
  74.       dup /UnderlinePosition known not
  75.        { dup /UnderlinePosition 3 index /FontBBox get
  76.          1 get 2 div put        % 1/2 the font descent
  77.        }
  78.       if
  79.       dup /UnderlineThickness known not
  80.        { dup /UnderlineThickness 3 index /FontBBox get
  81.          dup 3 get exch 1 get sub 20 div put    % 1/20 the font height
  82.        }
  83.       if
  84.       1 index /FontInfo get wcheck not { readonly } if
  85.       /FontInfo exch put
  86.     }
  87.    ifelse .buildfont1
  88.  } bind def
  89. % Define definefont.  This is a procedure built on an operator that
  90. % does all the error checking and key insertion.
  91. /.buildfontdict 3 dict
  92.     /.buildfont0 where { pop dup 0 /.buildfont0 load put } if
  93.     /.buildfont1 where { pop dup 1 /.buildfont1x load put } if
  94.     /.buildfont3 where { pop dup 3 /.buildfont3 load put } if
  95. def
  96. /definefont
  97.  { 1 dict begin count /d exch def    % save stack depth in case of error
  98.     { dup /FontType get .buildfontdict exch get exec
  99.       DISKFONTS
  100.        { FontFileDirectory 2 index known
  101.           { dup /FontFile FontFileDirectory 4 index get put
  102.       }
  103.      if
  104.        }
  105.       if
  106.       readonly
  107.     }
  108.    stopped
  109.     { count d sub { pop } repeat end /invalidfont signalerror }
  110.     { end dup FontDirectory 4 2 roll put }
  111.    ifelse
  112.  } odef
  113.  
  114.  
  115. % Ghostscript optionally can load individual CharStrings as they are needed.
  116. % (This is intended primarily for machines with very small memories.)
  117. % Initially, the character definition is the file position of the definition;
  118. % this gets replaced with the actual CharString.
  119. % Note that if we are loading characters lazily, CharStrings is writable.
  120.  
  121. % _Cstring must be long enough to hold the longest CharString for
  122. % a character defined using seac.  This is lenIV + 4 * 5 (for the operands
  123. % of sbw, assuming div is not used) + 2 (for sbw) + 3 * 5 (for the operands
  124. % of seac other than the character codes) + 2 * 2 (for the character codes)
  125. % + 2 (for seac), i.e., lenIV + 43.
  126.  
  127. /_Cstring 60 string def
  128.  
  129. % When we initially load the font, we call
  130. %    <index|charname> <length> /readstring|/readhexstring skip_C
  131. % to skip over each character definition and return the file position instead.
  132. % This substitutes for the procedure
  133. %    <length> string currentfile exch read[hex]string pop
  134. % What we actually store is fileposition * 1000 + length,
  135. %   negated if the string is stored in binary form.
  136.  
  137. /skip_C
  138.  { load exch dup 1000 ge 3 index type /nametype ne or
  139.     { % This is a Subrs string, or the string is so long we can't represent
  140.       % its length.  Load it now.
  141.       currentfile 3 1 roll string exch exec pop
  142.     }
  143.     { % Record the position and length, and skip the string.
  144.       dup currentfile fileposition 1000 mul add
  145.       2 index /readstring load eq { neg } if
  146.       3 1 roll
  147.       dup _Cstring length idiv
  148.        { currentfile _Cstring 3 index exec pop pop
  149.        } repeat
  150.       _Cstring length mod _Cstring exch 0 exch getinterval
  151.       currentfile exch 3 -1 roll exec pop pop
  152.     }
  153.    ifelse
  154.  } bind def
  155.  
  156. % Type1BuildChar calls load_C to actually load the character definition.
  157.  
  158. /load_C        % charindex fileposandlength ->
  159.  { exch Encoding exch get exch
  160.    read_C type1addpath
  161.  } bind def
  162.  
  163. /read_C        % charname fileposandlength -> charstring
  164.  { dup abs 1000 idiv FontFile exch setfileposition
  165.    CharStrings 3 1 roll
  166.    dup 0 lt
  167.     { neg 1000 mod string FontFile exch readstring }
  168.     { 1000 mod string FontFile exch readhexstring }
  169.    ifelse pop
  170.    dup 4 1 roll put
  171. % If the character is defined with seac, load its components now.
  172.    dup mark exch seac_C
  173.    counttomark
  174.     { StandardEncoding exch get dup CharStrings exch get
  175.       dup type /integertype eq { read_C } { pop } ifelse pop
  176.     } repeat
  177.    pop        % the mark
  178.  } bind def
  179.  
  180. /seac_C        % charstring -> achar bchar ..or nothing..
  181.  { dup length _Cstring length le
  182.     { 4330 exch _Cstring type1decrypt exch pop
  183.       dup dup length 2 sub 2 getinterval <0c06> eq    % seac
  184.        { dup length
  185.          Private /lenIV known { Private /lenIV get } { 4 } ifelse
  186.      exch 1 index sub getinterval
  187. % Parse the string just enough to extract the seac information.
  188. % We assume that the only possible operators are hsbw, sbw, and seac,
  189. % and that there are no 5-byte numbers.
  190.      mark 0 3 -1 roll
  191.       { exch
  192.          { { dup 32 lt
  193.               { pop 0 }
  194.           { dup 247 lt
  195.              { 139 sub 0 }
  196.              { dup 251 lt
  197.             { 247 sub 256 mul 108 add 1 1 }
  198.             { 251 sub -256 mul -108 add -1 1 }
  199.                ifelse
  200.              }
  201.             ifelse
  202.           }
  203.          ifelse
  204.            }            % 0
  205.            { mul add 0 }        % 1
  206.          }
  207.         exch get exec
  208.       }
  209.      forall pop
  210.      counttomark 1 add 2 roll cleartomark    % pop all but achar bchar
  211.        }
  212.        { pop    % not seac
  213.        }
  214.       ifelse
  215.     }
  216.     { pop    % punt
  217.     }
  218.    ifelse
  219.  } bind def
  220.  
  221. % Define an auxiliary procedure for loading a font.
  222. % If DISKFONTS is true:
  223. %    - Prevent the CharStrings from being made read-only.
  224. %    - Substitute a different CharString-reading procedure.
  225. % If the body of the font is encrypted with eexec, this is disabled,
  226. % because the implicit 'systemdict begin' hides the redefinitions.
  227. % We assume that:
  228. %    - The magic procedures (-|, -!, |-, and |) are defined with
  229. %    executeonly or readonly;
  230. %    - The contents of the reading procedures are as defined in bdftops.ps;
  231. %    - The font ends with
  232. %    <font> <Private> <CharStrings>
  233. %    readonly put noaccess|readonly put
  234. 4 dict begin
  235.  /dict            % leave room for FontFile
  236.   { 1 add dict
  237.   } bind def
  238.  /executeonly        % for reading procedures
  239.   { readonly
  240.   } def
  241.  /noaccess        % for Subrs strings and Private dictionary
  242.   { readonly
  243.   } def
  244.  /readonly        % for procedures and CharStrings dictionary
  245.   {    % We want to take the following non-standard actions here:
  246.       %   - If the operand is the CharStrings dictionary, do nothing;
  247.     %   - If the operand is a number (a file position replacing the
  248.     %    actual CharString), do nothing;
  249.     %   - If the operand is either of the reading procedures (-| or -!),
  250.     %    substitute a different one.
  251.     dup type /dicttype eq        % CharStrings or Private
  252.      { 1 index /CharStrings ne { readonly } if }
  253.      { dup type /arraytype eq        % procedure or data array
  254.         { dup length 5 eq 1 index xcheck and
  255.        { dup 0 get /string eq
  256.          1 index 1 get /currentfile eq and
  257.          1 index 2 get /exch eq and
  258.          1 index 3 get dup /readstring eq exch /readhexstring eq or and
  259.          1 index 4 get /pop eq and
  260.           { 3 get cvlit /skip_C cvx 2 packedarray cvx
  261.           }
  262.           { readonly
  263.           }
  264.          ifelse
  265.        }
  266.        { readonly
  267.        }
  268.       ifelse
  269.     }
  270.     { dup type /stringtype eq    % must be a Subr string
  271.        { readonly }
  272.       if
  273.     }
  274.        ifelse
  275.      }
  276.     ifelse
  277.   } bind def
  278. currentdict end /.loadfontdict exch def
  279. /.loadfont        % <file> .loadfont ->
  280.  { mark exch systemdict begin
  281.    % We really would just like systemdict on the stack,
  282.    % but fonts produced by Fontographer require a writable dictionary....
  283.    userdict begin
  284.    DISKFONTS { .loadfontdict begin } if
  285.     % We can't just use `run', because we want to check for
  286.     % .PFB files.  We can't save the packing status anywhere,
  287.     % so we need two separate control paths.
  288.     % Also, we would like to use `false /PFBDecode filter',
  289.     % but this occasionally produces a whitespace character as
  290.     % the first of an eexec section, so we can't do it.
  291.    currentpacking
  292.     { false setpacking
  293.        { dup read not { -1 } if
  294.          2 copy unread 16#80 eq { true /PFBDecode filter } if
  295.      cvx exec
  296.        } stopped    % split up `execute'
  297.       true setpacking
  298.       $error /newerror get and {handleerror} if
  299.     }
  300.     {  { dup read not { -1 } if
  301.          2 copy unread 16#80 eq { true /PFBDecode filter } if
  302.      cvx exec
  303.        } execute
  304.     }
  305.    ifelse
  306.    DISKFONTS { end } if
  307.    end end cleartomark
  308.  } bind def
  309.  
  310. % Define findfont so it tries to load a font if it's not found.
  311. /findfont
  312.  {    % Since PostScript has no concept of goto, or even blocks with
  313.     % exits, we use a loop as a way to get an exitable scope.
  314.     % The loop is never executed more than twice.
  315.     {
  316.     dup type /nametype ne    % Convert the key to a name for lookup.
  317.      { cvn } if
  318.  
  319.     dup FontDirectory exch known        % Already loaded?
  320.      { FontDirectory exch get exit }
  321.     if
  322.  
  323.     dup Fontmap exch known not    % Unknown font name.
  324.      { dup defaultfontname eq
  325.         { (Default font ) print cvx =only
  326.           ( not found in Fontmap!  Giving up.\n) print flush
  327.           1 .quit
  328.         } if
  329.        QUIET not
  330.         { (Substituting ) print defaultfontname cvx =only
  331.           ( for unknown font ) print dup == flush
  332.         } if
  333.        pop defaultfontname findfont exit
  334.      }
  335.     if
  336.  
  337.     dup Fontmap exch get
  338.  
  339.     % Check for a font alias.
  340.     dup type /nametype eq
  341.      { findfont
  342.        FontDirectory 2 index 2 index put
  343.        exch pop exit
  344.      }
  345.     if
  346.  
  347.     % If we can't find the file, substitute for the font.
  348.     findlibfile
  349.      { % Stack: fontname fontfilename fontfile
  350.        DISKFONTS
  351.         { 1 index (r) file
  352.           FontFileDirectory exch 4 index exch put
  353.         }
  354.        if
  355.        QUIET not
  356.         { (Loading ) print 2 index =only
  357.           ( font from ) print exch print (... ) print flush }
  358.         { exch pop }
  359.        ifelse
  360.        .loadfont
  361.        QUIET not
  362.         { vmstatus 3 { =only ( ) print } repeat
  363.           (done.\n) print flush
  364.         } if
  365.        % Check to make sure the font was actually loaded.
  366.        dup FontDirectory exch known { findfont exit } if
  367.  
  368.        (Loading ) print cvx =only
  369.        ( font failed, substituting ) print defaultfontname cvx =only
  370.        (.\n) print flush
  371.        defaultfontname findfont exit
  372.      }
  373.     if
  374.  
  375.     % findlibfile failed, substitute the default font.
  376.     % Stack: fontname fontfilename
  377.     (Can't find font file ) print print
  378.     dup defaultfontname eq
  379.      { ( for default font \() print cvx =only
  380.        (\)!  Giving up.\n) print flush 1 .quit
  381.      }
  382.      { ( for font ) print cvx =only
  383.        (, substituting ) print defaultfontname cvx =only
  384.        (.\n) print flush
  385.        defaultfontname findfont
  386.      }
  387.     ifelse
  388.     exit
  389.  
  390.     } loop        % end of loop
  391.  
  392.  } odef % bind def
  393.  
  394.  
  395. % The CharStrings for a Ghostscript font are a dictionary in which
  396. % the key is the character name, and the value is a compressed
  397. % representation of a path, as produced by type1imagepath.
  398. % For detailed information, see the book
  399. % "Adobe Type 1 Font Format", published by Adobe Systems Inc.
  400.  
  401. % Here is the BuildChar implementation
  402. % for Type 1 (Ghostscript standard) fonts.
  403. % The name Type1BuildChar is known to the interpreter.
  404.  
  405. /Type1BuildChar
  406.  { exch begin
  407.     dup Encoding exch get
  408.     dup CharStrings exch known not
  409.      { QUIET not
  410.         { (Substituting .notdef for ) print = flush
  411.     } { pop } ifelse
  412.        /.notdef
  413.      } if
  414.     currentdict /Metrics known
  415.      { dup Metrics exch known
  416.         { dup Metrics exch get .setmetrics } if
  417.      } if
  418.     CharStrings exch get
  419.     PaintType 0 ne
  420.      { 1 setmiterlimit 1 setlinejoin 1 setlinecap
  421.        currentdict /StrokeWidth known { StrokeWidth } { 0 } ifelse
  422.        setlinewidth
  423.      } if
  424.     dup type /stringtype eq        % encoded outline
  425.      { type1addpath pop }        % does a fill or stroke
  426.      { dup type /integertype eq        % file position for lazy loading
  427.         { load_C
  428.     }
  429.     { currentdict end systemdict begin begin
  430.       exec
  431.       end
  432.     }
  433.        ifelse
  434.      }
  435.     ifelse
  436.    end
  437.  } bind def
  438.  
  439. % Find all the precompiled font operators in systemdict.
  440.    systemdict
  441.     { exch =string cvs (.font_) anchorsearch
  442.        { pop pop exec    % execute the operator, returns the font dictionary
  443.          dup begin
  444.        Encoding type /stringtype eq
  445.         { Encoding cvn cvx exec /Encoding exch def
  446.         }
  447.        if
  448.        FontName exch
  449.      end definefont pop
  450.        }
  451.        { pop pop
  452.        }
  453.       ifelse
  454.     }
  455.    forall
  456.  
  457.  
  458.  
  459. % Define a procedure to load all known fonts.
  460. % This isn't likely to be very useful.
  461. /loadallfonts
  462.  { Fontmap { pop findfont pop } forall
  463.  } bind def
  464.